class StepAnalyzer {

    /**
     * 
     * @param {{stepName: string, urlPaths:[]}[]} stepConfigs 
     */
    constructor(stepConfigs) {
        this.stepConfigs = stepConfigs || [];
    }

    /**
     * 
     * @param {{name: string, apis: Api[]|Api[][]}[]} groups 
     * @returns {{name: string, apis: Api[]|Api[][]}[]}
     */
    analyze(groups) {
        if (this.stepConfigs.length === 0) {
            return groups;
        }
        groups.forEach(group => {
            /** @type {{url: URL}[]} */
            const apis = [];
            group.apis.forEach(item => {
                if (Array.isArray(item)) {
                    apis.push(...item);
                } else {
                    apis.push(item);
                }
            });
            let step = this.stepConfigs.find(stepConfig => {
                return stepConfig.urlPaths.every(urlPath => apis.map(api => api.url.pathname).includes(urlPath));
            });
            if (step) {
                group.name = `${group.name}: ${step.stepName}`;
            }
        });
        return groups;
    }

}

module.exports = StepAnalyzer;